package com.sky.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.exception.OrderBusinessException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class CheckOutOfRangeUtil {
    /**
     * 详细路线规划
     * @param address 收货地址
     * return jsonObject 请求返回的json对象
     */
    public JSONObject getcheckOutOfRange(String key, String AdminLngLat, String userLngLat) {
        // 路线规划
        Map<String, String> RouteMap = new HashMap<>();
        RouteMap.put("key",key);
        RouteMap.put("origin",AdminLngLat);
        RouteMap.put("destination",userLngLat);
        String show_fields = "cost,duration,navi,polyline";
        RouteMap.put("show_fields",show_fields);
        RouteMap.put("alternative_route","3");
        // 获取电动车路线规划
        String json = HttpClientUtil.doGet("https://restapi.amap.com/v5/direction/electrobike?", RouteMap);
        JSONObject jsonObject = JSON.parseObject(json);
        if(!jsonObject.getString("status").equals("1")){
            throw new OrderBusinessException(MessageConstant.DISTRIBUTION_ROUTE_PLANNING_FAILED);
        }
        return jsonObject;
    }

    /**
     * 获得地址经纬度坐标
     * @param map 封装基础地址信息
     * @param address 地址信息
     * @param city 城市信息
     * @return jsonObject 请求返回的json对象
     */
    public JSONObject getlocation(String address, String city, String key){
        // 封装地址信息
        Map RangeMap = new HashMap();
        RangeMap.put("key",key);
        RangeMap.put("address",address);
        RangeMap.put("city",city);
        // 获取经纬度坐标
        String shopCoordinate = HttpClientUtil.doGet("https://restapi.amap.com/v3/geocode/geo?", RangeMap);
        JSONObject jsonObject = JSON.parseObject(shopCoordinate);
        // 判断是否解析成功。status = 1 表示解析成功
        if(!jsonObject.getString("status").equals("1")){
            throw new OrderBusinessException(MessageConstant.STORE_ADDRESS_RESOLUTION_FAILED);
        }
        return jsonObject;
    }
}
